package com.hfzy.ihk.web.oauth.server.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.common.constants.GlobalStatic;
import com.hfzy.ihk.common.util.http.HttpHelper;
import com.hfzy.ihk.common.util.image.CaptchaUtils;
import com.hfzy.ihk.common.util.string.StringUtil;
import com.hfzy.ihk.common.web.enums.LoginErrorMsgEnum;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.facade.userCenter.service.IUserService;
import com.hfzy.ihk.web.oauth.server.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用于集成spring security 前端控制器
 * </p>
 *
 * @author wws
 * @since 2018-12-28
 */
@Controller
@Slf4j
//@SessionAttributes("authorizationRequest")
public class LoginController {

    @Autowired
    IUserService userService;

    @Autowired
    UserMapper userMapper;

    /**
     * 自定义登录页面
     * 这里定义一个参数param 用于判断是否有错误
     * @return
     */
    @GetMapping({"/login","/"})
    public ModelAndView login(ModelAndView model,HttpServletRequest request) {
        AuthenticationException ae = (AuthenticationException) request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
        log.info("==========》自定义登录接口回调的地址："+request.getHeader("referer"));
       if(ae!=null) {
           String message = ae.getMessage();
           String ms = "";
           model.addObject("param1", message);
           if(StringUtil.isNotNull(message)&&LoginErrorMsgEnum.isDefined(message)){
               ms =  LoginErrorMsgEnum.fromKey(message);
           }else if(StringUtil.isNotEmpty(message)&&message.equals("Bad credentials")){
               ms = LoginErrorMsgEnum.PASSWORD_NOT_FIND.getDesc();
           }else{
               ms =  LoginErrorMsgEnum.LOGIN_FAIL.getDesc();
           }

           model.addObject("param1", ms);

       }else{
           model.addObject("param1", "");
       }
        model.setViewName("login");
        return model;
    }




    @GetMapping("/index")
    public String index() {
        return "index";
    }







    /**
     * 登出处理
     */
    @RequestMapping("/logout")
    public ModelAndView logoutView(HttpServletRequest request, HttpServletResponse response){
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null){
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return new ModelAndView("redirect:/login");
    }
}
